/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.4.0
  * @date    10/15/2010
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "stm32f10x.h"

#include "Console.h"
#include "memory.h"
#include "spi_flash.h"
#include "ELM327.h"
#include "gsm_modem.h"

/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  static char dbg=0;
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
    if (dbg==1) return;
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
//unsigned short mircoSec = 0;
//unsigned short gRTCSec = 0;
void SysTick_Handler(void)
{
    mircoSec++;
    gSystick++;
    if(mircoSec >= 1000)
    {
        gRTCSec++;
        mircoSec = 0;
    }
    if((gSystick % 10) == 0)
    {
        SPI_Flash_Hook();
    }

	if (gSystick_GSMDLY!=0)	
		gSystick_GSMDLY--;
}
unsigned short gRTCCnt = 0;
void RTC_IRQHandler(void)
{
  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    /* Clear the RTC Second interrupt */
    RTC_ClearITPendingBit(RTC_IT_SEC);

    /* Toggle LED1 */
    //STM_EVAL_LEDToggle(LED1);

    /* Enable time update */
   //TimeDisplay = 1;

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    gRTCCnt++;
  }
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 

void USART2_IRQHandler(void)
{
  /*
	if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
        ELM327_UART_IRQ_Rx_Callback();
	}
	if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
	{
        USART_ClearITPendingBit(USART2, USART_IT_TXE);
        ELM327_UART_IRQ_Tx_Callback();
	}
  */

//Support to GPRS_U_BLOX
  	if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{

        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
        GSM_UART_IRQ_Rx();
	}
	if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
	{

        USART_ClearITPendingBit(USART2, USART_IT_TXE);
        GSM_UART_IRQ_Tx();
	}
}

void USART3_IRQHandler(void)
{
	if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
	{
		//Console_IRQ_Rx_Callback();
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
        UART_IRQ_Rx();
	}
	if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
	{
		//Console_IRQ_Tx_Callback();
        USART_ClearITPendingBit(USART3, USART_IT_TXE);
        UART_IRQ_Tx();
	}
}

/**
  * @brief  This function handles SPI1 request.
  * @param  None
  * @retval : None
  */
void SPI1_IRQHandler(void)
{
	SPI1_IRQ_CALLBACK();
}

void UART4_IRQHandler(void)
{
	if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
	{
		//Console_IRQ_Rx_Callback();
        USART_ClearITPendingBit(UART4, USART_IT_RXNE);
        GPS_UART_IRQ_Rx();
	}
	if (USART_GetITStatus(UART4, USART_IT_TXE) != RESET)
	{
		//Console_IRQ_Tx_Callback();
        USART_ClearITPendingBit(UART4, USART_IT_TXE);
        GPS_UART_IRQ_Tx();
	}
}

void UART5_IRQHandler(void)
{
	if (USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
	{
        USART_ClearITPendingBit(UART5, USART_IT_RXNE);
        ELM327_UART_IRQ_Rx_Callback();
	}
	if (USART_GetITStatus(UART5, USART_IT_TXE) != RESET)
	{
        USART_ClearITPendingBit(UART5, USART_IT_TXE);
        ELM327_UART_IRQ_Tx_Callback();
	}
}

void EXTI9_5_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line9) != RESET)
	{
		EXTI_ClearITPendingBit(EXTI_Line9);
		GPS_PPS_IRQ_CallBack();
	}
}

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
